/**
 * Created by ahwwl on 17-9-9.
 */
(function (original) {
    jQuery.fn.clone = function () {
        var result = original.apply(this, arguments),
            my_textareas = this.find('textarea').add(this.filter('textarea')),
            result_textareas = result.find('textarea').add(result.filter('textarea')),
            my_selects = this.find('select').add(this.filter('select')),
            result_selects = result.find('select').add(result.filter('select'));

        for (var i = 0, l = my_textareas.length; i < l; ++i) $(result_textareas[i]).val($(my_textareas[i]).val());
        for (var i = 0, l = my_selects.length; i < l; ++i)
        {
            $(result_selects[i]).val($(my_selects[i]).val()).trigger("change");
            // result_selects[i].selectedIndex = my_selects[i].selectedIndex;
        }
        return result;
    };
}) (jQuery.fn.clone);
(function ($) {
    $.ley = {
        default_config:{
            type: 0,
            title: '提示',
            closeBtn: true,
            shade: 0.65,
            area: ['100%', "100%"],
            content: '',
            clone:false,
            btn: ['确定','取消'],
            id:"",
            zIndex: 10000,
            yes: null,
            cancel: null,
            success: null,
            end:null,
            skin:null,
            scrollbar:false,
            time:0
        },
        msg:function (s) {
            var default_s = {
                type: 0,
                title: '',
                shade:0,
                closeBtn: false,
                skin:"ley-dialog-msg",
                scrollbar:true,
                time:2000
            };
            s = $.extend({},default_s,s);
            this.open(s);
        },
        prompt:function (content,yes,cancel) {
            var _this = this;
            if(!content)
            {
                content = '<input type="text" name="" value="" class="ley-dialog-input">';
            }
            var default_s = {
                type: 0,
                title: '信息',
                area: ['300px', "160px"],
                content: content,
                clone:false,
                yes: function (index,obj,e) {
                    var value = obj.find(".ley-dialog-input").val();
                    if(yes)
                    {
                        var r = yes(value);
                        if(r === false)
                        {
                            return false;
                        }
                    }
                    obj.close();
                },
                cancel: cancel
            };
            _this.open(default_s);
        },
        open:function (s) {
            var _this = this;
            _this.default_config.id = "ley_dialog_"+(new Date()).valueOf();
            s = $.extend({},_this.default_config,s);
            var ley_dialog = $("<div class='ley-dialog'></div>");
            var ley_dialog_main = $("<div class='ley-dialog-main'></div>");
            var ley_dialog_backdrop = $("<div class='ley-dialog-backdrop'></div>");
            var ley_dialog_header = $("<div class='ley-dialog-header'></div>");
            var ley_dialog_body = $("<div class='ley-dialog-body'></div>");
            var ley_dialog_footer = $("<div class='ley-dialog-footer'></div>");
            var ley_dialog_close = $('<button class="ley-dialog-close"  type="button">×</button>');
            var ley_dialog_title = $('<h4 class="ley-dialog-title"></h4>');
            ley_dialog_header.append(ley_dialog_close).append(ley_dialog_title);
            ley_dialog_main.append(ley_dialog_header).append(ley_dialog_body).append(ley_dialog_footer);
            ley_dialog.append(ley_dialog_main).append(ley_dialog_backdrop).appendTo("body");
            if(s.title)
            {
                ley_dialog_title.html(s.title);
            }
            s.closeBtn?ley_dialog_close.show():ley_dialog_close.hide();
            ley_dialog_backdrop.css("opacity",s.shade);
            s.content = s.clone&& $.type(s.content) === "object"?s.content.clone(true,true):s.content;
            if(s.type === 0 )
            {
                ley_dialog_body.append(s.content);
            }
            else if(s.type === 1)
            {
                if($.type(s.content) === "object")
                {
                    s.content.show();
                }
                ley_dialog_body.append(s.content);
            }else if(s.type === 2)
            {
                var modal_inflame = $('<iframe src=""  scrolling="auto" frameborder="0"></iframe>');
                ley_dialog_body.append(modal_inflame);
                if(s.content)
                {

                    modal_inflame.attr("src",s.content);

                }
            }
            // ley_dialog_body.append(s.content);
            if(s.btn.length>0)
            {
                for(var i =0;i<s.btn.length;i++)
                {
                    var btn = $('<a href="javascript:void 0;" class="ley-dialog-btn"></a>');
                    btn.html(s.btn[i]);
                    if(i === 0)
                    {
                        btn.addClass("ley-dialog-btn-success");
                    }else if(i === 1)
                    {
                        btn.addClass("ley-dialog-btn-danger");
                    }else
                    {
                        btn.addClass("ley-dialog-btn-primary");
                    }
                    ley_dialog_footer.append(btn);
                }
            }
            if(s.skin)
            {
                ley_dialog.addClass(s.skin)
            }
            if(s.area)
            {
                var w = "100%";
                var h = "100%";
                if($.isArray(s.area))
                {
                    w = s.area[0];
                    if(s.area.length>1)
                    {
                        h = s.area[1];
                    }
                }else if($.type(s.area) == "string")
                {
                    w = s.area;
                }
                w = w.replace(";","");
                h = h.replace(";","");
                ley_dialog_main.css({"width":w,"height":h});
                ley_dialog_main.attr({"data-width":w,"data-height":h});
            }
            ley_dialog.css("z-index",_this.getZIndex());
            ley_dialog_backdrop.css("z-index",_this.getZIndex());
            ley_dialog_main.css("z-index",_this.getZIndex());
            s.scrollbar?null:_this.setScrollbars();
            ley_dialog_close.click(function () {
                _this.close(ley_dialog);
            });
            ley_dialog_footer.find(".ley-dialog-btn").click(function (e) {
                var index = $(this).index();
                if(index === 0 )
                {
                    if(s.yes)
                    {
                        s.yes(index,ley_dialog,e)
                    }else if(s["btn"+(index+1)])
                    {
                        s["btn"+(index+1)](index,ley_dialog,e)
                    }
                }else if(index === 1)
                {
                    if(s.cancel)
                    {
                        s.cancel(index,ley_dialog,e)
                    }else if(s["btn"+(index+1)])
                    {
                        s["btn"+(index+1)](index,ley_dialog,e)
                    }
                    else{
                        _this.close(ley_dialog);
                    }
                }else if(s["btn"+(index+1)])
                {
                    s["btn"+(index+1)](index,ley_dialog,e)
                }
                return false;
            });
            ley_dialog_backdrop.click(function (e) {
                _this.close(ley_dialog);
            });
            ley_dialog[0].close = function () {
                _this.close(ley_dialog);
            };

            ley_dialog[0].timeobj = s.time?setTimeout(function () {
                ley_dialog.close();
            },s.time):null;
            if(s.success)
            {
                s.success(ley_dialog);
            }
            if(s.end)
            {
                s.end(ley_dialog);
            }

            _this.resize();
        },
        getClone:function (content) {
            content = content.clone(true,true);
            content.show();
            return content;
        },
        getZIndex:function()
        {
            this.default_config.zIndex += 1;
            return this.default_config.zIndex;
        },
        setScrollbars:function () {
            $("body").addClass('ley-dialog-open');
        },
        close:function (obj) {
            try
            {
                if(obj[0].timeobj)
                {
                    clearTimeout(obj[0].timeobj);
                }
                obj.remove();
                if($(".ley-dialog").not(".ley-dialog-msg").length<1)
                {
                    $("body").removeClass('ley-dialog-open');
                }
            }
            catch (e)
            {

            }
        },
        resize:function () {
            $(".ley-dialog").each(function () {
                var w1 = $(window).width();
                var h1 = $(window).height();

                var w2 = $(this).find(".ley-dialog-main").attr("data-width");
                var h2 = $(this).find(".ley-dialog-main").attr("data-height");
                var w3 = "";
                var h3 = "";

                if(w2.substr(w2.length-2,2) === "px")
                {

                    var w4 = w2.substr(0,w2.length-2);
                    if(w4>w1)
                    {
                        w3 = "100%";
                    }else{
                        w3 = w4;
                    }
                }else{
                    if(w2>w1)
                    {
                        w3 = "100%";
                    }else{
                        w3 = w2;
                    }
                }
                if(h2.substr(h2.length-2,2) === "px")
                {
                    var h4 = h2.substr(0,h2.length-2);
                    if(h4>h1)
                    {
                        h3 = "100%";
                    }else{
                        h3 = h4;
                    }
                }else{
                    if(h2>h1)
                    {
                        h3 = "100%";
                    }else{
                        h3 = h2;
                    }
                }
                if(w3)
                {
                    $(this).find(".ley-dialog-main").width(w3);
                }
                if(h3)
                {
                    $(this).find(".ley-dialog-main").height(h3);
                }
            });
        }
    };
    $.fn.close = function () {
        $(this).each(function () {
            if($(this).hasClass("ley-dialog"))
            {
                $(this)[0].close();
            }
        });
    };
    $(window).resize(function () {
        $.ley.resize();
    });
})(jQuery);